<?php
	//header('content-type:text/html;charset=utf8');
	//require 'config.inc.php';
	

	class Page{
		//每页显示的记录数
		private $page_rows;
		//得到总记录数查询语句
		private $sql;
		//要得到总页数
		public $page_count = 0;
		public $page_curr = 1;
		//上一面与下一页中间需要显示的页码数
		private $show_pages_num;
		//给类增加一个GET参数列表的字符串,但不包括page本身
		private $gets_str;
		
		function __construct($sql,$page_rows=10,$show_pages_num = 5){
			$this->page_rows = $page_rows;
			$this->sql = $sql;
			$this->show_pages_num = $show_pages_num;
			$this->get_page_count();//得到总页数
			$this->get_page_curr();//处理好当前页码
			$this->exec_gets();//处理一下$_GET参数,把它转换为字符串
			//print_r($_GET);
			
		}
		private function exec_gets(){
			//?page=3&uid=4&a=123  //转换成类似于这样的字符串  uid=4&a=3
			if(!empty($_GET)){
				foreach($_GET as $k=>$v){
					if($k != 'page'){
						$this->gets_str .= '&'.$k.'='.$v;//&uid=4&a=123
					}
				}
			}
			
		}
	/**
	 * 得到总页数
	 */
	private function get_page_count(){
		//$sql = 'select id from remain';//只查ID列,比*快很多,省内存.
		//$link = mysqli_connect(DB_HOST, DB_USER, DB_PWD, DB_NAME);
		//$result = mysqli_query($link, $this->sql);
		global $link;
		$stmt = $link->prepare($this->sql);
		$stmt->execute();
		$row_count = $stmt->rowCount();//得到查询结果的总数行
		//$row_count = mysqli_num_rows($result);//得到查询结果的总数行
		//$page_count = ceil($row_count/$page_num);//得到总页数
		//ysqli_close($link);
		$this->page_count = ceil($row_count/$this->page_rows);//得到总页数
		//$this->page_count = $this->page_count < 1 ? 1 : $this->page_count;//如果是0页,则当成1页看待
	}
	/**
	 * 获取并处理当前页函数
	 */
	private function get_page_curr(){
		//得到$page_curr 当前GET参数中的页码(当前页)
		$page_curr = empty($_GET['page']) ? 1 : $_GET['page'];
		//echo $page_curr;
		//如果是数字,不管是小数还是整数还是合法的数字的字符串,都把它转成整数.
		if(is_numeric($page_curr)){
			$page_curr = intval($page_curr);
		}else{
			$page_curr = 1;
		}
		//echo $page_curr;
		//echo $this->page_count;
		//如果当前页码大于总页数,则显示最后一页,如果当前页码小于1则为1
		if($page_curr > $this->page_count) {
			$page_curr = $this->page_count;
		}
		if($page_curr < 1){
			 $page_curr = 1;
		}
		//把当前页码返回
		//echo $page_curr;
		$this->page_curr = $page_curr;
	}
		/**
	 * 打印分页功能
	 */
	function print_page(){
		echo '<ul id="showpage">';
		//显示首页,上一页
		if($this->page_curr > 1){
			echo '<a href="?page=1'.$this->gets_str.'"><li>首页</li></a>';
			echo '<a href="?page='.($this->page_curr-1).$this->gets_str.'"><li>上一页</li></a>';
		}
		//第一种情况:总页数小于要显示的页码
		if($this->page_count <= $this->show_pages_num){
			for($i = 1 ; $i <= $this->page_count ; $i++){
				if($this->page_curr == $i){
					echo '<li class="curr">'.$i.'</li>';
				}else{
					echo '<a href="?page='.$i.$this->gets_str.'"><li>'.$i.'</li></a>';
				}
			}
		}
		//第二种情况:当显示前几页(<=我想在中间显示的页码数/2)时
		elseif($this->page_curr <= floor($this->show_pages_num/2)){
			for($i = 1 ; $i <= $this->show_pages_num ; $i++){
				if($this->page_curr == $i){
					echo '<li class="curr">'.$i.'</li>';
				}else{
					echo '<a href="?page='.$i.$this->gets_str.'"><li>'.$i.'</li></a>';
				}
			}
		}
		//第三种情况:当显示后几页(>=总页数-我想在中间显示的页码数/2)时
		elseif($this->page_curr > ($this->page_count - floor($this->show_pages_num/2))){
			for($i = $this->page_count-$this->show_pages_num+1 ; $i <= $this->page_count ; $i++){
				if($this->page_curr == $i){
					echo '<li class="curr">'.$i.'</li>';
				}else{
					echo '<a href="?page='.$i.$this->gets_str.'"><li>'.$i.'</li></a>';
				}
			}
		}
		//第四种情况:正常的中间页...前后都有:我想在中间显示的页码数/2
		else{
			$start = $this->page_curr- floor($this->show_pages_num/2);
			for($i = $start ; $i < $start+$this->show_pages_num ; $i++){
				if($this->page_curr == $i){
					echo '<li class="curr">'.$i.'</li>';
				}else{
					echo '<a href="?page='.$i.$this->gets_str.'"><li>'.$i.'</li></a>';
				}
			}
		}
	//显示下一页,尾页
		if($this->page_curr < $this->page_count){
			echo '<a href="?page='.($this->page_curr+1).$this->gets_str.'"><li>下一页</li></a>';
			echo '<a href="?page='.$this->page_count.$this->gets_str.'"><li>尾页</li></a>';
		}
		echo '<li>第'.$this->page_curr.'/'.$this->page_count.'页</li>';
		echo '</ul>';
	}
}

	
?>